package com.pengtu.service.asset;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.asset.book.BkInformationDao;
import com.pengtu.dao.asset.book.BkLendDao;
import com.pengtu.dao.asset.book.BkSnDao;
import com.pengtu.entity.asset.book.BkInformation;
import com.pengtu.entity.asset.book.BkLend;
import com.pengtu.entity.asset.book.BkSn;

@Component
@Transactional
public class BookManager {

	/**
	 * 记录日志
	 */
	private static Logger logger = Logger.getLogger(BookManager.class);

	/**
	 * 注入数据访问对象
	 */

	private BkInformationDao bkInformationDao;
	private BkLendDao bkLendDao;
	private BkSnDao bksnDao;

	@Autowired
	public void setBkInformationDao(BkInformationDao bkInformationDao) {

		this.bkInformationDao = bkInformationDao;
	}
	
	@Autowired
	public void setBkLendDao(BkLendDao bkLendDao) {
		this.bkLendDao = bkLendDao;
	}
		
	@Autowired
	public void setBksnDao(BkSnDao bksnDao) {
		this.bksnDao = bksnDao;
	}


	/* 操作BkInformation */


	/**
	 * 保存或修改对象
	 */
	public void SaveOrUpdate(BkInformation bkInformation) {

		if (StringUtils.isEmpty(bkInformation.getId())) {
			logger.debug("saveBkInformation access!");
			bkInformationDao.save(bkInformation);
		} else {
			logger.debug("updateBkInformation access!");
			bkInformationDao.update(bkInformation);
		}

	}

	/**
	 * 删除对象【将删除标记改为1】
	 * 
	 * @param bkInfoId
	 *            【主键Id】
	 */
	public void deleteBI(String bkInfoId) {

		logger.warn("deleteBkinformation access!");
		bkInformationDao.delete(bkInfoId);
	}

	/**
	 * 根据属性名查询
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly = true)
	public BkInformation findByBIproperty(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id")) {
			return bkInformationDao.get(value);
		}
		return bkInformationDao.findUniqueBy(propertyName, value);
	}
	
	/**
	 * 
	 * checkBookNo:
	 * 适用:验证图书编号是否重复
	 * @param string
	 * @param bookNo
	 * @param oldBookNo
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean checkBookNo( String bookNo, String oldBookNo) {
		// TODO Auto-generated method stub
		 return bkInformationDao.isPropertyUnique("bookNo",bookNo, oldBookNo);
	}

	/**
	 * 分页获取数据列表
	 * 
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<BkInformation> findPage(final Page<BkInformation> page) {
		return bkInformationDao.findPage(page);
	}

	/**
	 * 加模糊查询的分页显示
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<BkInformation> findFuzzyQuery(final Page<BkInformation> page,
			final QueryFilter queryFilter) {
		return bkInformationDao.findPage(page, queryFilter);
	}

	/* 操作BkLend */
	/**
	 * 保存对象
	 */
	public void Save(BkLend bkLend,String booksId) {
		
		if (StringUtils.isEmpty(bkLend.getId())) {
			BkSn bksn = new BkSn();
			booksId = booksId.substring(0, booksId.lastIndexOf("_"));
			String [] books = booksId.split("_");
			List<BkLend> lendList = new ArrayList<BkLend>();
			for (int i = 0; i < books.length; i++) {
				BkLend bkll = new BkLend();
				bksn = this.getBkSn("id", books[i]);
				bksn.setStatus("1");
				bkll.setBksn(bksn);
				bkll.setCreateDate(bkLend.getCreateDate());
				bkll.setDelFlag(bkLend.getDelFlag());
				bkll.setLendDate(bkLend.getLendDate());
				bkll.setLendPerson(bkLend.getLendPerson());
				bkll.setPreparationAlsoDate(bkLend.getPreparationAlsoDate());
				bkll.setRemark(bkLend.getRemark());
				bkll.setReturnDate(bkLend.getReturnDate());
				bkll.setStatus(bkLend.getStatus());
				bkll.setSubscriber(bkLend.getSubscriber());
				lendList.add(bkll);
			}
			for (int i = 0; i < lendList.size(); i++) {
				bkLendDao.save(lendList.get(i));
				logger.debug("saveBkLend access!");
			}
		}
	}
	
	public void update(String lendBook){
		
		if(StringUtils.isNotEmpty(lendBook)){
			lendBook = lendBook.substring(0, lendBook.lastIndexOf("_"));
			String [] lendIdList = lendBook.split("_");
			for (int i = 0; i < lendIdList.length; i++) {
				BkLend lend = this.findByBLproperty("id",lendIdList[i]);
				lend.getBksn().setStatus("0");
				lend.setReturnDate(new Date());
				bkLendDao.update(lend);
			}
		}
	}

	/**
	 * @param bkLendId
	 *            【主键Id】
	 */
	public void deleteBL(String bkLendId) {

		logger.warn("deleteBkLend access!");
		bkLendDao.delete(bkLendId);
	}

	/**
	 * 根据属性名查询
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly = true)
	public BkLend findByBLproperty(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id")) {
			return bkLendDao.get(value);
		}
		return bkLendDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 分页获取数据列表
	 * 
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<BkLend> findBLPage(final Page<BkLend> page) {
		return bkLendDao.findPage(page);
	}

	/**
	 * 加模糊查询的分页显示
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<BkLend> findBLFuzzyQuery(final Page<BkLend> page,
			final QueryFilter queryFilter) {
		return bkLendDao.findPage(page, queryFilter);
	}

	/**********************操作BkSn*******************/
	
	public void saveBkSn(BkSn bkSn) {
		bksnDao.save(bkSn);
	}
	
	public void deleteBkSn(String id) {
		bksnDao.delete(id);
	}
	
	@Transactional
	public BkSn getBkSn(String propertyName, String value) {
		if(propertyName.equalsIgnoreCase("id"))
			return  bksnDao.get(value);
		return bksnDao.findUniqueBy(propertyName, value);	
	}
	
	@Transactional(readOnly=true)
	public List<BkSn> findListBkSn() {
		return bksnDao.find();
	}

	@Transactional(readOnly=true)
	public Page<BkSn> findPageBksn(final  Page<BkSn> page, final QueryFilter queryfilter) {
		// TODO Auto-generated method stub
		return bksnDao.findPage(page, queryfilter);
	}
	
	
	
}

